# admin.py
from django.contrib import admin
from .models import ScheduledTask, TaskExecutionLog, TaskRegistry

@admin.register(ScheduledTask)
class ScheduledTaskAdmin(admin.ModelAdmin):
    list_display = ('name', 'task_function', 'is_active', 'schedule_type')
    list_filter = ('is_active', 'schedule_type')
    search_fields = ('name',)
    list_editable = ('is_active',)
    ordering = ('id',)

    def formfield_for_choice_field(self, db_field, request, **kwargs):
        if db_field.name == "task_function":
            registered_tasks = TaskRegistry.get_registered_tasks()
            kwargs['choices'] = [(key, value) for key, value in registered_tasks.items()]
        return super().formfield_for_choice_field(db_field, request, **kwargs)




@admin.register(TaskExecutionLog)
class TaskExecutionLogAdmin(admin.ModelAdmin):
    list_display = ('task', 'execution_time', 'status', 'result', 'error')
    list_filter = ('status', 'execution_time')
    search_fields = ('task__name', 'result', 'error')

